﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.IO;
using WpfHero;

namespace Tutorial
{
    /// <summary>
    /// Contains logic for directory browsing.
    /// </summary>
    public class SongBrowser
    {
        /// <summary>
        /// Contains the list of the subdirectories on the current level.
        /// </summary>
        public ObservableCollection<MyDirectory> SubDirectories
        { get; private set; }

        private MyDirectory _currentDirectory;

        /// <summary>
        /// Gets or sets the current directory
        /// </summary>
        public MyDirectory CurrentDirectory
        {
            get { return _currentDirectory; }
            set
            {
                // The current directory changes
                _currentDirectory = value;
                // Clear the list of subdirectories.
                SubDirectories.Clear();


                if (Directory.GetDirectoryRoot(CurrentDirectory.FullPath) !=
                                            CurrentDirectory.FullPath)
                    // Add the parent directory item (..)
                    SubDirectories.Add(new MyDirectory(
                        Directory.GetParent(CurrentDirectory.FullPath).FullName,
                        true)
                    );

                // Add each directory
                foreach (var dir in Directory.GetDirectories(
                                        CurrentDirectory.FullPath))
                    SubDirectories.Add(new MyDirectory(dir, false));
            }
        }

        /// <summary>
        /// Creates a new instance of the SongBrowser class.
        /// </summary>
        public SongBrowser()
        {
            SubDirectories = new ObservableCollection<MyDirectory>();
            CurrentDirectory = new MyDirectory(Directory.GetCurrentDirectory(),
                                            false);
        }
    }

    /// <summary>
    /// Contains information about a directory.
    /// </summary>
    public class MyDirectory
    {
        /// <summary>
        /// Creates a new instance of the MyDirectory class.
        /// </summary>
        /// <param name="fullPath">The full path of the directory.</param>
        /// <param name="isParent">Whether the directory is leading to the parent directory.</param>
        public MyDirectory(string fullPath, bool isParent)
        {
            this.FullPath = fullPath;
            this.IsParent = isParent;
        }

        /// <summary>
        /// Gets the name of the directory, without the path.
        /// </summary>
        public string Name
        {
            get
            {
                return IsParent ?
                            ".." :
                            FullPath.Split(new char[] { '\\' }).Last();
            }
        }

        /// <summary>
        /// Gets or sets the full path of the directory.
        /// </summary>
        public string FullPath { get; private set; }

        /// <summary>
        /// Gets or sets whether the current directory is leading to the parent directory.
        /// </summary>
        public bool IsParent { get; set; }

        /// <summary>
        /// Gets whether the directory has any songs (Checks for the existence of a notes.mid file).
        /// </summary>
        public bool HasSong
        {
            get { return File.Exists(Utils.GetSongPath(FullPath)); }
        }
    }
}